Lab1


4230269821_4230269821  นาย บวรสันติ์ นวราช (COM22420) (27/6/2545 (8:47:56))
(SM=1, CM=7, ST=6, KY=0, TR=11:10)

MiniQuiz
Mini-Quiz : Q1=3,Q2=1,Q3=2,Q4=1,Q5=4  (5.0 คะแนน)


ได้ 5 คะแนน
Applet
Source Code
/*
 *----------------------------------
 *   2110101 Computer Programming
 *   Lab #1 : Edit+Compile+Launch
 *   Author : somchaip@chula.ac.th
 *   Date   : 21/06/2545
 *----------------------------------
 */
import jlab.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Mandelbrot extends Applet implements MouseListener, MouseMotionListener {

  private int curWidth, curHeight;
  private Image imageBuffer = null;
  private Graphics gBuffer;
  private Cursor cursorWait, cursorCross;
  private boolean isDragging, enableMouse, redraw;
  private double xs = -2.0, xe = 1.0;
  private double ys = -1.5, ye = 1.5;
  private int ixs, ixe, iys, iye;

  //---------------------------------------------------------
  //  add your code here
  private void drawMandelbrot(Graphics g) {
  // display boundary
  curWidth = getSize().width;
  curHeight = getSize().height;
  System.out.println("(" + xs + "," + xe+"),(" +
                           ys + "," + ye+")");
  double tx0, tx, ty, r;
  double dx = (xe-xs) / curWidth;
  double dy = (ye-ys) / curHeight;
  int n, px, py;
  float curColor, prevColor = 0.0f;
  enableMouse = false;
  setCursor(cursorWait);
  for (double y = ys; y <= ye; y += dy) {
    for (double x = xs; x <= xe; x += dx) {
      n = 0;
      tx = ty = 0.0;
      do {
        tx0 = tx * tx - ty * ty;
        ty = 2 * tx * ty + y;
        tx = tx0 + x;
       } while (tx0 < 4.0 && ++n < 256);
       curColor = n / 256.0f;
       if (curColor != prevColor) {
         gBuffer.setColor(Color.getHSBColor(curColor,
                          0.8f, (1.0f - curColor * curColor)));
                          g.setColor(gBuffer.getColor());
                          prevColor = curColor;
                        }
                        px = (int)((x - xs) / dx);
                        py = (int)((y - ys) / dy);
                        gBuffer.drawLine(px, py, px + 1, py);
                        g.drawLine(px, py, px + 1, py);
                        
                   }
                }
          setCursor(cursorCross);
          enableMouse = true;
       }
        
  //---------------------------------------------------------

  public void paint(Graphics g) {
    update(g);
  }

  public void update(Graphics g) {
    int newWidth = getSize().width;
    int newHeight = getSize().height;
    
    if (redraw) {
      drawMandelbrot(g);
      redraw = false;
    } else if (imageBuffer == null || newWidth != curWidth || newHeight != curHeight) {
      imageBuffer = createImage(newWidth, newHeight);
      gBuffer = imageBuffer.getGraphics();
      drawMandelbrot(g);
    }
    g.drawImage(imageBuffer, 0, 0, this);
    if (isDragging) {
      g.setColor(Color.white);
      if (ixs < ixe) {
        if (iys < iye) g.drawRect(ixs, iys, (ixe - ixs), (iye - iys));
        else g.drawRect(ixs, iye, (ixe - ixs), (iys - iye));
      } else {
        if (iys < iye) g.drawRect(ixe, iys, (ixs - ixe), (iye - iys));
        else g.drawRect(ixe, iye, (ixs - ixe), (iys - iye));
      }
    }
  }

  public void init() {
    addMouseListener(this);
    addMouseMotionListener(this);
    cursorWait = new Cursor(Cursor.WAIT_CURSOR);
    cursorCross = new Cursor(Cursor.CROSSHAIR_CURSOR);
  }
  
  public void start() {
    isDragging = false;
    redraw = false;
  }
  
  public void mousePressed(MouseEvent e) {
    e.consume();
    if (enableMouse) {
      ixs = e.getX();
      iys = e.getY();
    }
  }
  
  public void mouseReleased(MouseEvent e) {
    int tmp;
    e.consume();
    if (enableMouse) {
      ixe = e.getX();
      iye = e.getY();
      if (ixs > ixe) {
        tmp = ixs; ixs = ixe; ixe = tmp;
      }
      if (iys > iye) {
        tmp = iys; iys = iye; iye = tmp;
      }
      
      isDragging = false;
      // redraw the set
      double dx = (xe-xs) / curWidth;
      double dy = (ye-ys) / curHeight;
      double tx = xs, ty = ys;
      int idx = ixe - ixs;
      int idy = iye - iys;
      // ignore if the selected region is too small
      if (idx > 10 || idy > 10) {
        if (idx > idy)
          iye = iys + idx;
        else
          ixe = ixs + idy;
        xs = tx + ixs * dx;
        xe = tx + ixe * dx;
        ys = ty + iys * dy;
        ye = ty + iye * dy;
        redraw = true;
        repaint();
      }
    }
  }
  
  public void mouseDragged(MouseEvent e) {
    e.consume();
    if (enableMouse) {
      ixe = e.getX();
      iye = e.getY();
      isDragging = true;
      repaint();
    }
  }

  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}
  public void mouseClicked(MouseEvent e) {}
  public void mouseMoved(MouseEvent e) {}


  // main method
  public static void main(String[] args) {
    String title = "JLab : Mandelbrot";
    // set up window frame
    Frame frame = new Frame(title);
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
        }
      }
    );
    Applet myApp = new Mandelbrot();
    frame.add(myApp, BorderLayout.CENTER);
    frame.setSize(200, 200);
    frame.setVisible(true);
    try {JLabIO.setWindowAlwaysOnTop(title); } catch (Error e) {}
    myApp.init();
    myApp.start();
  }
}